/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate;

import java.util.ArrayList;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author Igor Kovb
 */
@Stateless
@LocalBean
public class RequestDAO {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    

    ElementDAOClass<Request> coreRequestDAO;
    ElementDAOClass<RequestStatus> coreRequestStatusDAO;

    public RequestDAO() {
        coreRequestDAO = new ElementDAOClass<Request>(Request.class);
        coreRequestStatusDAO = new ElementDAOClass<RequestStatus>(RequestStatus.class);
    }

     //-----------------------------------Request DAO section

    public List<Request> getAllRequests() {
        List<Request> res = (List<Request>) coreRequestDAO.getAllElements();
        return res;
    }

    public Request getRequestByID(int pID) {
        Request res = (Request) coreRequestDAO.getElementByID(pID);
        return res;
    }

    public void addAndSaveNewRequest(Request newRequest) {
        coreRequestDAO.addElement(newRequest);
    }

    public Request createEmptyRequest() {
        return new Request();
    }

    public void deleteRequest(Request requestToDelete) {
        coreRequestDAO.deleteElement(requestToDelete);
    }

    public void updateRequest(Request requestToUpdate) {
        coreRequestDAO.updateElement(requestToUpdate);
    }


    //----------------------------------RequestStatus DAO section


    public List<RequestStatus> getAllRequestStatuses() {
        List<RequestStatus> res = (List<RequestStatus>) coreRequestStatusDAO.getAllElements();
        return res;
    }

    public RequestStatus getRequestStatusByID(int pID) {
        RequestStatus res = (RequestStatus) coreRequestStatusDAO.getElementByID(pID);
        return res;
    }

    public void addAndSaveNewRequestStatus(RequestStatus newRequestStatus) {
        coreRequestStatusDAO.addElement(newRequestStatus);
    }

    public RequestStatus createEmptyRequestStatus() {
        return new RequestStatus();
    }

    public void deleteRequestStatus(RequestStatus RequestStatusToDelete) {
        coreRequestStatusDAO.deleteElement(RequestStatusToDelete);
    }

    public void updateRequestStatus(RequestStatus RequestStatusToUpdate) {
        coreRequestStatusDAO.updateElement(RequestStatusToUpdate);
    }

    public List<Request> getAllRequestsForUser(User currentUser) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public List<Request> getRequests(String squery, Map<String, Object> parameters) {
        List<Request> res = null;
        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            // creating a query			
            Query query = session.createQuery(squery);

            for (Map.Entry<String, Object> param : parameters.entrySet()) {
                if (param.getValue() instanceof ArrayList) {
                    query.setParameterList(param.getKey(), (ArrayList)param.getValue());
                } else {
                    query.setParameter(param.getKey(), param.getValue());
                }
            }
                

            res = query.list();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
            return res;
        }

    }

    

}
